﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Net.Mail;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace Gmail
{
    public partial class FormMain : Form
    {
        public FormMain()
        {
            InitializeComponent();
        }

        private void BtnBrowseClick(object sender, EventArgs e)
        {
            openFileDialog.ShowDialog();
            var fileName = openFileDialog.FileName;
            txtAttachment.Text = fileName;
        }

        private void BtnSendMailClick(object sender, EventArgs e)
        {
            var thread = new Thread(SendMail);
            timer.Enabled = true;
            thread.Start();
        }

        private void SendMail()
        {
            const string host = "smtp.gmail.com";
            const string password = "hungct1991";
            const int port = 25;

            var to = txtTo.Text;
            var from = txtFrom.Text;
            var cc = txtCC.Text;
            var bcc = txtBcc.Text;
            var subject = txtSubject.Text;
            var body = txtBody.Text;

            var message = new MailMessage(from, to, subject, body) {IsBodyHtml = true};

            if (cc != "") message.CC.Add(cc);
            if (bcc != "") message.Bcc.Add(bcc);

            var client = new SmtpClient(host, port)
                             {
                                 DeliveryMethod = SmtpDeliveryMethod.Network,
                                 EnableSsl = true,
                                 Credentials = new NetworkCredential(@from, password)
                             };
            client.Send(message);
        }
    }
}
